/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaproject;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author hanxiao
 */
public class preWindow extends JFrame{
    public static String sk;
    JTextField userContent;
    int num = 1;
    
    public preWindow(){
        this.setBounds(800,360, 400, 300);
//        this.setLayout(new BorderLayout());
        this.setTitle("Login Window Interface ");
//        this.setBackground(Color.red);             
        this.setVisible(true);
        this.setResizable(false);
        
        JPanel p1 = new JPanel();
        JLabel welcome = new JLabel("weclome");
        welcome.setForeground(Color.blue);
        welcome.setFont(new Font("Arial",Font.BOLD,36));
//        this.add(p1,BorderLayout.NORTH);
//        p1.setLayout(new FlowLayout());
        p1.add(welcome);
     
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        JLabel userName = new JLabel("UserName      ");
        userContent = new JTextField(10);        //要存的内容！
//      jt.addKeyListener(new monitor());
        p2.add(userName);
        p2.add(userContent);

        JPanel p3 = new JPanel();
        p3.setLayout(new FlowLayout());
        JButton login = new JButton("Login");
        JButton exist = new JButton("Exist");
        p3.add(login);
        p3.add(new JLabel("       "));
        p3.add(exist);
        
        exist.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                  System.exit(-1);
            }

        });
        
        login.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){                
                Connection con = ImportDb.getConnection();
                String name = userContent.getText();
                String sql_pre = "select user_id from user";
                String sql = "insert into user values(?,?,?)";                
                PreparedStatement pst = null;
                ResultSet rst = null;
                int flag =0;
                try {
                    pst = con.prepareStatement(sql_pre);
                    rst = pst.executeQuery();
                    
                    while(rst.next()){
                        if(num!=rst.getInt(1)){
                            break;
                        }
                        num++;
                    }
                    pst = con.prepareStatement(sql);
                    pst.setInt(1, num);
                    pst.setString(2, name);
                    pst.setString(3, "0");
                    flag = pst.executeUpdate();      
                    
                    System.out.println("insert" + num + " / " + name + "into user");
                    
                } catch (SQLException ex) {
                    System.out.println("login actionPerformed method" + ex);
                }finally{
                    try {
                        con.close();
                    } catch (SQLException ex) {
                        System.out.println("close connection error!! "+ex);
                    }
                }
                new Window(name);
                stop();
            }
        });
        
        JPanel j = new JPanel();
        j.setLayout(new GridLayout(5, 1));
        
        j.add(p1);
        j.add(new JLabel());
        j.add(p2);
        j.add(new JLabel());
        j.add(p3);
        this.add(j);
  
    }
    
    public void stop(){
        this.setVisible(false);
    }

    class monitor extends  KeyAdapter{
       public  void KeyPressed(KeyEvent e){
           Object s = e.getKeyCode();
           sk =  (String) s;
           System.out.println(sk);
       }
    }
   
}
